package com.geek.admin.common.utils;

import java.math.BigDecimal;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.util.ObjectUtils;

/**
 * @author gaozeyu
 */
public class UtilObj {
    public UtilObj() {
    }

    public static boolean isEmpty(Object obj) {
        return ObjectUtils.isEmpty(obj);
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static Object convert(Object obj, Class<?> type) {
        if (!type.isPrimitive()) {
            if (String.class.equals(type)) {
                return "" + obj;
            }

            if (Integer.class.equals(type) || Integer.TYPE.equals(type)) {
                return ((Number)obj).intValue();
            }

            if (Long.class.equals(type) || Long.TYPE.equals(type)) {
                return ((Number)obj).longValue();
            }

            if (BigDecimal.class.equals(type)) {
                return new BigDecimal(obj + "");
            }

            if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) {
                return Boolean.valueOf(obj + "");
            }
        }

        return obj;
    }

    public static boolean isBasicType(Object obj) {
        return null == obj ? false : ClassUtils.isPrimitiveOrWrapper(obj.getClass());
    }
}
